package view
{
	
	import event.GotoClickButtonEvent;
	
	import flash.events.MouseEvent;
	
	import model.component.ChapterContainer;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	import spark.components.Button;
	import spark.components.supportClasses.SkinnableComponent;
	
	
	[Event(name="gotoClickButton", type="event.GotoClickButtonEvent")]
	public class Reader extends SkinnableComponent
	{
		[SkinPart] public var backtoCatalogButton:Button;
		[SkinPart] public var chapterList:ChapterContainer;
		
		[Bindable]
		public var chapters:ArrayCollection;
		
		override protected function getCurrentSkinState():String
		{
			return super.getCurrentSkinState();
		} 
		
		override protected function partAdded(partName:String, instance:Object) : void
		{
			super.partAdded(partName, instance);
			
			if (instance == backtoCatalogButton)
			{
				backtoCatalogButton.addEventListener(MouseEvent.CLICK, handleGotoButtonClick);
			}
		}

		
		protected function handleGotoButtonClick(event:MouseEvent):void
		{
			Alert.show("Your progress will be lost. Do you still want to go back to Catalog?", "You have been warned!", Alert.YES | Alert.NO, this, alertListener, null, Alert.NO);		
		}
		
		private function alertListener(e:CloseEvent):void {
			if (e.detail==Alert.YES) {
				chapterList.refreshBook();
				dispatchEvent(new GotoClickButtonEvent(GotoClickButtonEvent.GOTOCLICKBUTTONEVENT));
			}
		}

			
		
		override protected function partRemoved(partName:String, instance:Object) : void
		{
			super.partRemoved(partName, instance);
		}
		
	}
}